<?
ob_start();
include("common/header.html");
include("common/left.html");
$out_html=ob_get_contents();
ob_end_clean();
include("common/page.php");
include("class/OrderRecord.php");
include("class/ProOrder.php");
include("class/SimpleExcel.class.php");
$atype= 'sale';// 导出文件名判断条件
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s= new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID=$_SESSION['sid'];

//查询合并的salerid
if($_SESSION['sdtype']==3){//合并过的终端并且是主体
    $sql = "select preSalerID from pft_terminal_share where nowSalerID={$_SESSION['saccount']}";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $total_salerid .= $row['preSalerID'].',';
    }
    $total_salerid = rtrim($total_salerid,',');
}else{
    $total_salerid = $_SESSION['account'];
}
//查询所有景区的名字
$sql = "select title,salerid from uu_land where salerid in ($total_salerid)";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
    $ltitle_salerid[$row['salerid']]= $row['title'];
}
if($_POST['salerid']!=''){//选择了指定的景区供应商
    $salerid = $_POST['salerid'];
}else{
    foreach($ltitle_salerid as $k => $v){
        $salerid .= $k.',';
    }
    $salerid = rtrim($salerid,',');
}
$pOrder=saleProOrderObject($memberID,$s,$_SESSION['sdtype'], $salerid);

$v['title2']="分销订单";
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:15;

// 导出订单设置
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
    $currentPage=1;
    $pageSize=99999;
    set_time_limit(1800);
}

$r=$pOrder->_orderlist($currentPage,$pageSize);
if($_GET['a']){
    echo '<pre>';
    print_r($r);
    echo '</pre>';
}
$aid = $orderCount = $ticketCount = $allMoney = 0;// 订单统计信息

$showTel = array('602902','603782','603783',"600626","617368","861943",'863271','616877','864436','864286'
,'864436','864286',
);

foreach($r as $key=>$row){

    if(!$ltitle_salerid[(int)$row['mainOrder']->UUsalerid]){
        unset($r[$key]);
        continue;
    }

    // 直接供应商查看价格
    if($_SESSION['dtype']==2){
        // $r[$key]['mainOrder']->UUtprice = $s->get_Dynamic_Price_Merge((string)$row['mainOrder']->UUpid,(string)$row['mainOrder']->UUbegintime,1);
        $prices = explode(",", (string )$r[$key]['mainOrder']->UUaids_price);
        $r[$key]['mainOrder']->UUtprice = count($prices)>1 ?  $prices[0] : $r[$key]['mainOrder']->UUtprice;
    }

    /*
    if($r[$key]['mainOrder']->UUlid!=72 && !in_array($_SESSION['account'],$showTel)){
        if($r[$key]['mainOrder']->UUstatus!=1){
            $r[$key]['mainOrder']->UUordertel=substr_replace((string)$r[$key]['mainOrder']->UUordertel,"****",3,4);
        }
    }
    */
    if(!$aid){ // 作为景区看到分销商永远是发布产品的供应商
        if((string)$row['mainOrder']->UUaids){
            $aids = explode(',',(string)$row['mainOrder']->UUaids);
            $aid = $aids[0];
        }else{
            $aid = (string)$row['mainOrder']->UUsellid;
        }
    }

    $r[$key]['totalPrice'] = ((string)$row['mainOrder']->UUtprice*(string)$row['mainOrder']->UUtnum);
    // 统计订单信息
    ++$orderCount;
    $ticketCount+=(string)$row['mainOrder']->UUtnum;
    $allMoney+=((int)$row['mainOrder']->UUtprice*(int)$row['mainOrder']->UUtnum)/100;

}

$arr = array();

foreach($r as $key => $row){
    // 处理
    $concat = (string)$row['mainOrder']->UUconcat_id;
    $orderid = (string)$row['mainOrder']->UUordernum;
    if($concat==0){
        $arr[] = $r[$key];// 主订单
        continue;
    }
    if($concat!=$orderid){
        $tmp[$key]= $r[$key];
        continue;
    }
    // 主订单并且含有从订单
    if(is_array($tmp))
    foreach($tmp as $k=>$data){
        if((string)$data['mainOrder']->UUconcat_id==$orderid && (string)$data['mainOrder']->UUordernum!=$orderid){
            $r[$key]['childOrder'][] = $data;
            unset($tmp[$k]);
        }
    }
    $arr[] = $r[$key];
}
$r = $arr;


if($aid){
    $sel="select id,dname from pft_member where id=$aid";
    $GLOBALS['le']->query($sel);
    $GLOBALS['le']->fetch_assoc();
    $dname =$GLOBALS['le']->f('dname');
}

include("loadExcel_bl.php");
echo $out_html;

// 判断订单是否为主订单  true为主订单
function isMainOrder($field){
    return ($field=='main' || $field!=0 ) ? true:false;
}
// 复票订单主订单
function echoMainOrder($obj){
    return ((String)$obj->UUconcat_id=='main') ? (String)$obj->UUordernum:(String)$obj->UUconcat_id;
}

$temp_str = $pOrder->orderlistTotal();
$temp_arr = explode(',', $temp_str);
$count = $temp_arr[0];

$arr_salerid=array(501610,501612);
$uuStatus = array(
    0=>"未使用",
    1=>"<em class='green'>已使用</em>",
    2=>"已过期",
    3=>"<em class='red'>已取消</em>",
    4=>"被替代",
    5=>"被修改",
    6=>"已撤销",
    7=>"部分使用"
);
$v = array();
$v['from'] = 'sale';
?>
    <title>已卖出的产品</title>
    <script src="js/ui/PopWindow.js" type="text/javascript"></script>
    <script src="js/zy_Calendar.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="css/default.css"/>
    <link rel="stylesheet" type="text/css" href="css/page.css"/>
    <link rel="stylesheet" type="text/css" href="css/sell_product.css"/>
    <link rel="stylesheet" href="css/buyProduct.css" />
    <div class="rigArea">
    <div class="topTit"><span class="txt"><strong>已<?=($v['from'] == 'sale')? '卖出':'买到'?>的产品</strong></span></div>
    <div id="searchWrap" class="searchWrap">
        <form action="<?=$_SERVER['PHP_SELF']?>" class="report" id="orderSearchForm">
            <input type="hidden" name="act" id="act" />
            <input type="hidden" name="atype" value="<?=$atype?>" />
            <input type="hidden" name="currentPage" value="1"/>
            <input type="hidden" name="status" id="status" value="<?=$_REQUEST['status']?>"/>
            <div class="line linea">
                <span class="lt">订单时间：</span>
                <select class="timeSelect controls" name="xtimes" id="">
                    <option <?=$_REQUEST['xtimes']=="1"?"selected":""?> value="1">下单时间</option>
                    <option <?=$_REQUEST['xtimes']=="2"?"selected":""?> value="2">预计游玩</option>
                    <option <?=$_REQUEST['xtimes']=="3"?"selected":""?> value="3">完成时间</option>
                </select>
                <input type="text" name="begintime" id="op" class="inputText date controls" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)" value="<?=$_REQUEST['begintime']?>"/>
                <span>至</span>
                <input type="text" name="endtime" id="ed" class="inputText date controls"  onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)" value="<?=$_REQUEST['endtime']?>"/>
                <span class="sst">汇总时间：</span>
                <a href="#" title="yestoday" class="qc quickDate fqc">昨天</a>
                <a href="#" title="today" class="qc quickDate">今天</a>
                <!-- <a href="#" title="tomorrow" class="qc quickDate">明天</a> -->
                <a href="#" title="week" class="qc quickDate">本周</a>
                <a href="#" title="lastWeek" class="qc quickDate">上周</a>
                <a href="#" title="month" class="qc quickDate">本月</a>
                <a href="#" title="lastMonth" class="qc quickDate">上月</a>
                <i class="calendarBtn c1"></i>
                <i class="calendarBtn c2"></i>
            </div>
            <div class="line lineb">
                <span class="lt">查询条件：</span>
                <select class="condSelect controls" name="tjs" id="condSelect">
                    <option <?=$_REQUEST['tjs']=="0"?"selected":""?> value="0">订单号</option>
                    <option <?=$_REQUEST['tjs']=="1"?"selected":""?> value="1">产品名称</option>
                    <option <?=$_REQUEST['tjs']=="2"?"selected":""?> value="2">取票人姓名</option>
                    <option <?=$_REQUEST['tjs']=="3"?"selected":""?> value="3">取票人手机</option>
                    <option <?=$_REQUEST['tjs']=="6"?"selected":""?> value="6">远端订单号</option>
                </select>
                <input id="condInput" type="text" class="condText controls" value="<?=$_REQUEST['txt']?>" name="txt"/>
                <?$DistorsNames = ($v['from'] == 'sale') ? $disList:$supList;?>
                <?if($_SESSION['memberID']==1):?>
                    <span class="lt">供应商：</span>
                    <select class="fenxiaoSelect controls" name="supplier" id="">
                        <option value="">请选择供应商</option>
                        <?foreach($DistorsNames as $key=> $cn):?>
                            <option value="<?=$key?>" <?if($_REQUEST['supplier']==$key)echo 'selected="selected"'?>><?=$cn?></option>
                        <?endforeach;?>
                    </select>
                <?
                endif;
                $_REQUEST['status']=is_array($_REQUEST['status']) ? $_REQUEST['status'] : array();
                $_REQUEST['payinfo']=is_array($_REQUEST['payinfo']) ? $_REQUEST['payinfo'] : array();
                ?>
            </div>
            <div class="line linec">
                <span class="lt">订单状态：</span>
                <label for="labcheckaa"><input type="checkbox" class="checkbox" name="status[]" value="0" <?=(in_array(0,$_REQUEST['status']))?"checked":""?>/><span class="checkboxT">未使用</span></label>
                <label for="labcheckab"><input type="checkbox" class="checkbox" name="status[]" value="1" <?=(in_array(1,$_REQUEST['status']))?"checked":""?>/><span class="checkboxT">已使用</span></label>
                <label for="labcheckac"><input type="checkbox" class="checkbox" name="status[]" value="2" <?=(in_array(2,$_REQUEST['status']))?"checked":""?>/><span class="checkboxT">已过期</span></label>
                <label for="labcheckad"><input type="checkbox" class="checkbox" name="status[]" value="3" <?=(in_array(3,$_REQUEST['status']))?"checked":""?>/><span class="checkboxT">已取消</span></label>
            </div>
            <div class="line linec">
                <span class="lt">支付方式：</span>
                <label for="labcheckba"><input type="checkbox" name="payinfo[]" <?=(in_array(0,$_REQUEST['payinfo']))?"checked":""?> value="0" class="checkbox"/><span>帐户余额</span></label>
                <label for="labcheckbb"><input type="checkbox" name="payinfo[]" <?=(in_array(1,$_REQUEST['payinfo']))?"checked":""?> value="1" class="checkbox"/><span>在线支付</span></label>
                <label for="labcheckbc"><input type="checkbox" name="payinfo[]" <?=(in_array(2,$_REQUEST['payinfo']))?"checked":""?> value="2" class="checkbox"/><span>授信支付</span></label>
            </div>
            <div class="formBtnW">
                <input type="submit" value="查 询" class="queryBtn searchBtn" />
                <a href="javascript:void(0);" class="dcexcleBtn searchBtn" id="excel">导出excle</a>
            </div>
        </form>
    </div>
    <div class="totalSumW">
        <p><span class="span1">订单数<i class="i ia green"><?=$orderCount?></i>单</span><span>数 量<i class="i ib red"><?=$ticketCount?></i></span><span>总金额<i class="i ic orange"><?=$allMoney?></i>元</span></p>
    </div>
    <table class="proTable fixed">
        <thead id="proTableHead">
        <th class="col_1">订单号</th>
        <th class="col_2">产品信息</th>
        <th class="col_3">取票人信息</th>
        <th class="col_4">分销商</th>
        <th class="col_5">支付方式</th>
        <th class="col_6">订单状态</th>
        <th class="col_7">总金额</th>
        <th class="col_8">操作</th>
        </thead>
        <tbody>
        <!-- 第1个产品 -->
        <? //print_r($r);
        foreach($r as $row):
            $childNum = count($row['childOrder']);
            ?>
            <tr class="listHead">
                <td colspan="8">
                    <div>
                        <a href="scenicDetail.html?lid=<?=(string)$row['mainOrder']->UUlid?>" title="<?=(string)$row['mainOrder']->UUltitle?>" class="title" target="_blank"><?=(string)$row['mainOrder']->UUltitle?></a>
                        <p class="rt">
                            <span class="span">下单时间：<?=(string)$row['mainOrder']->UUordertime?></span>
                            <span class="span">有效期：<?=(string)$row['mainOrder']->UUbegintime?> 至 <?=(string)$row['mainOrder']->UUendtime?></span>
                            <span class="span span3">验证时间：<?=(string)$row['mainOrder']->UUdtime!="0000-00-00 00:00:00"?(string)$row['mainOrder']->UUdtime:"未验证"?></span>
                        </p>
                    </div>
                </td>
            </tr>
            <tr class="listBody <?if(!$childNum)echo 'borderBottom';?>" >
                <td class="col_1 col" rowspan="<?=$childNum + 1?>">
                    <?if((string)$row['mainOrder']->UUp_type=='F'):?>
                        <a target="_blank" href="orderView.html?from=<?=$v['from']?>&ordernum=<?=$row['mainOrder']->UUordernum?>&type=<?=$row['mainOrder']->UUp_type?>&pack=1" class="ordernb"><?=(string)$row['mainOrder']->UUordernum?></a>
                    <?else:?>
                        <?if(isMainOrder($row['mainOrder']->UUconcat_id)):?>
                            <a target="_blank" href="orderView.html?from=<?=$v['from']?>&ordernum=<?=$row['mainOrder']->UUordernum?>&type=<?=$row['mainOrder']->UUp_type?>&pack=1" class="ordernb"><?=(string)$row['mainOrder']->UUordernum?></a>
                        <?else:?>
                            <a target="_blank" href="orderView.html?from=<?=$v['from']?>&ordernum=<?=$row['mainOrder']->UUordernum?>&type=<?=$row['mainOrder']->UUp_type?>" class="ordernb"><?=(string)$row['mainOrder']->UUordernum?></a>
                        <?endif;?>
                    <?endif;?>
                    <?if((string)$row['mainOrder']->UUremotenum){?>
                        <span class="f_orderNum"><?=(string)$row['mainOrder']->UUremotenum?></span>
                    <?}?>
                </td>
                <td class="col_2 col">
                    【<?=(string)$row['mainOrder']->UUttitle?>】<span class="numPrice"> * <i><?=(string)$row['mainOrder']->UUtnum?></i> 张 * &yen;<i><?=(string)$row['mainOrder']->UUtprice/100?></i></span>
                </td>
                <td class="col_3 col" rowspan="<?=$childNum + 1?>">
                    <p class="name"><?=(string)$row['mainOrder']->UUordername?></p>
                    <p class="tell"><?php
                        $str=(string)$row['mainOrder']->UUordertel;
                        if(in_array($_SESSION['account'],$showTel)){
                            print_r($str);
                        }else{
                            print_r(substr_replace($str,'****',3,4));
                        } ?></p>
                </td>

                <td class="col_4 col" rowspan="<?=$childNum + 1?>">
                    <?=$dname?>
                </td>
                <td class="col_5 col" rowspan="<?=$childNum + 1?>">
                    <?=OrderRecord::$paymodeT[(string)$row['mainOrder']->UUpmode]?>
                    <?if($row['mainOrder']->UUordermode!=0){?>
                        </br><em class="fuchsia">(游客预订)</em>
                    <?}?>
                </td>
                <td class="col_6 col" rowspan="<?=$childNum + 1?>">
				<span rowspan="<?=$row['totalOrder']?>">
                    <?if((string)$row['mainOrder']->UUp_type=='F' && (string)$row['mainOrder']->UUstatus==7)://套票子票有一张使用则显示套票已使用?>
                        <p><?=$uuStatus[1]?></p>
                    <?else:?>
                        <p>
                            <?=$uuStatus[(string)$row['mainOrder']->UUstatus]?>
                            <?if((string)$row['mainOrder']->UUstatus==2 && (string)$row['mainOrder']->UUpaystatus==2) echo '<br/><span style="color:red;">(未支付)</span>';?>
                        </p>
                    <?endif;//套票子票有一张使用则显示套票已使用?>
                    <p class="green">
                        <?if($row['mainOrder']->UUstatus==0){?>
                            <?=OrderRecord::$paystatusT[(string)$row['mainOrder']->UUpaystatus]?>
                        <?}?>
                    </p>
                </td>
                <td class="col_7 col" rowspan="<?=$childNum + 1?>">

                    <?if(!isMainOrder($row['mainOrder']->UUconcat_id)):?>
                        合计<span class="totalPrice">&yen;<i><?=((int)$row['mainOrder']->UUtprice*(int)$row['mainOrder']->UUtnum)/100?></i></span>
                    <?else:?>
                        <?
                        $total = 0;
                        foreach($row['childOrder'] as $cr){
                            // $total = $total + (string)$cr['totalPrice']/100 ;
                            $total = $total + ((int)$cr['mainOrder']->UUtprice*(int)$cr['mainOrder']->UUtnum)/100;
                        }
                        $total += ((int)$row['mainOrder']->UUtprice*(int)$row['mainOrder']->UUtnum)/100;
                        ?>
                        合计<span class="totalPrice">&yen;<i><?=$total?></i></span>
                    <?endif;?>

                </td>
                <td class="col_8 col" rowspan="<?=$childNum + 1?>">
                    <?
                    // 订单号
                    $order = (string)$row['mainOrder']->UUordernum;
                    //查看
                    $view='orderView.html?from='.$v['from'].'&ordernum=';
                    $view.= $order.'&type='.$row['mainOrder']->UUp_type;
                    if((string)$row['mainOrder']->UUp_type=='F' || isMainOrder($row['mainOrder']->UUconcat_id))
                        $view.='&pack=1';
                    echo '<a target="_blank" href="'.$view.'">查看</a><br/>';
                    //验证
                    //echo '<a target="_blank" href="terminal_chk.html?sid='.$row['mainOrder']->UUlid.'&tel='.(int)$row['mainOrder']->UUcontacttel.'">验证</a><br/>';
                    ?>
                    <?if($_SESSION['dtype']==2 && in_array($_SESSION['account'],$arr_salerid)):?>
                        <span><?=$row['mainOrder']->UUcode?></span>
                    <?endif;?>
                </td>
            </tr>
            <?foreach($row['childOrder'] as $cr):?>
            <tr class="listBody borderBottom borderTop">
                <td class="col_2 col">
                    【<?=(string)$cr['mainOrder']->UUttitle?>】
                    <span class="numPrice"> * <i><?=(string)$cr['mainOrder']->UUtnum?></i> 张 * &yen;<i><?=(string)$cr['mainOrder']->UUtprice/100?></i></span>
                </td>
            </tr>
        <?endforeach;//子票?>
            <tr class="marTop"><td colspan="8"></td></tr>
        <?endforeach;?>
        </tbody>
    </table>
    <!-- <?php echo $count,'--',$currentPage,'--',$pageSize,'--',$order,'--',$by;?>   -->

    <?page($count,$currentPage,$pageSize,$order,$by);?>
    <style>.whichPageWrap{ display:block;}</style>
    </div>
    <div id="theadCopy" class="theadCopy">
        <ul>
            <li>订单号</li>
            <li>产品信息</li>
            <li>取票人信息</li>
            <? echo  ($v['from']=='buy') ? '<li>供应商</li>':'<li>分销商</li>';?>
            <li>支付方式</li>
            <li>订单状态</li>
            <li>总金额</li>
            <li>操作</li>
        </ul>
    </div>
    <script type="text/javascript" src="js/buyProduct.js"></script>
    <script type="text/javascript" src="js/plugin/date/moment.min.js"></script>
    <script>
        $("#excel").click(function(){

            $("#act").val("Export");
            $("form.report").submit();
            $("#act").val("");
        });
    </script>
<?
include("common/footer.html");
?>